
/**   
 * @Title: SignUpTask.java 
 * @Package: com.rename.icode.task 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-12 下午5:05:55 
 * @version  
 */


package com.rename.icode.task;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ParseException;
import android.os.AsyncTask;
import android.widget.Toast;

import com.rename.icode.R;
import com.rename.icode.activity.ActivitySupport;
import com.rename.icode.activity.SignUpActivity;
import com.rename.icode.comm.ApplicationContext;
import com.rename.icode.comm.Constants;
import com.rename.icode.manager.AppConfigManager;
import com.rename.icode.manager.UserManager;
import com.rename.icode.model.User;
import com.rename.icode.utils.HttpURLTools;
import com.rename.icode.utils.LogUtil;
import com.rename.icode.utils.StringUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-12 下午5:05:55 
 * @version V1.0
 */

public class SignUpTask extends AsyncTask<String, Integer, Integer> {
	private final static String TAG = "SignUpTask";
	private ProgressDialog pd;
	private ActivitySupport activity;
	private User mUser;
	private Context context;

	public SignUpTask(ActivitySupport as) {
		this.pd = as.getProgressDialog();
		this.activity = as;
		this.context = ApplicationContext.get();
	}

	public void initUser(User u) {
		this.mUser = u;
	}
	
	@Override
	protected void onPreExecute() {
		pd.setTitle(context.getResources().getString(R.string.please_wait));
		pd.setMessage(context.getResources().getString(R.string.in_resister));
		pd.show();
		super.onPreExecute();
	}

	@Override
	protected Integer doInBackground(String... params) {
		return signUp();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
	}

	@Override
	protected void onPostExecute(Integer result) {
		pd.dismiss();
		
		SignUpActivity signup;
		switch (result) {
		case Constants.SERVER_SUCCESS: // 服务器端登录验证成功
			AppConfigManager.getInstance().saveLoginConfig();
			signup = (SignUpActivity)activity;
			signup.notifySignUpSucc();//通过handler发送消息去启动LoginTask启动客户端登录
			break;
		case Constants.SERVER_FAIL:
			LogUtil.i(TAG, "sign up fail");
			signup = (SignUpActivity)activity;
			signup.notifySignUpFail();
			break;
		}
		super.onPostExecute(result);
	}

	//
	private Integer signUp() {
		String username = mUser.getName();
		String password = mUser.getPassword();
		String phone = mUser.getPhone();
		String result = null;
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("username", username);
		params.put("password", password);
		params.put("phone", phone);
		HttpResponse response = HttpURLTools.sendHttpPost(Constants.REGISTER_URL,
				params);
		LogUtil.i(TAG, "username is " + username);
		LogUtil.i(TAG, "password is " + password);
		LogUtil.i(TAG, "phone is " + phone);
		LogUtil.i(TAG, "response is " + response);
		if (response != null) {
			try {
				result = EntityUtils.toString(response.getEntity());
				LogUtil.i(TAG, "sign up result is " + result);
				if ((Constants.SERVER_SUCCESS + "").equals(result)) {
					return Constants.SERVER_SUCCESS;
				}
			} catch (ParseException | IOException e) {
				e.printStackTrace();
			}
		} else {
			return Constants.SERVER_FAIL;
		}

		// 保存登录信息
		AppConfigManager.getInstance().setPassword(password);
		AppConfigManager.getInstance().setUsername(username);

		return Constants.SERVER_FAIL;
		
//		return Constants.SERVER_FAIL;
	}
}
